package com.zgjkhis.modules.outpatient.service.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zgjkhis.modules.outpatient.domain.Account;
import com.zgjkhis.modules.outpatient.dto.AccountDto;
import com.zgjkhis.modules.outpatient.dto.AccountQueryCriteria;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yxl
 * @Description
 * @Date 2021/7/19 11:16
 **/
@Repository
public interface AccountMapper extends BaseMapper<Account> {
    /**
     * @Author yzq
     * @Description 根据id，查询患者信息
     * @Date 2021/9/3 10:39
     * @Param [id]
     * @return com.zgjkhis.modules.outpatient.dto.AccountDto
     **/
    @Select("<script>select t.*,IFNULL(m.member_name,'普通会员') as membershipName from (select * from his_account where id=#{id} and pharmacy_id=#{pharmacyId}) t\n" +
            "left join his_member m on t.membership_type=m.id\n</script>")
    AccountDto findDetailById(@Param("id") String id,@Param("pharmacyId") String pharmacyId);

    /**
     * @Author yzq
     * @Description 分页查询用户信息
     * @Date 2021/9/3 10:55
     * @Param [criteria]
     * @return java.util.List<com.zgjkhis.modules.outpatient.dto.AccountDto>
     **/
    @Select("<script>select t.*,IFNULL(m.member_name,'普通会员') as membershipName from his_account t " +
            "left join his_member m on t.membership_type=m.id " +
            " where 1=1 and t.pharmacy_id = #{account.pharmacyId} " +
            "<if test=\"account.memberId != null and account.memberId != '' and account.memberId !='0' \">\n" +
            "   and m.id=#{account.memberId}\n" +
            "</if>\n" +
            "<if test=\"account.startTime !=null \">\n" +
            "and t.create_time  &gt; #{account.startTime}\n" +
            "</if>\n" +
            "<if test=\"account.endTime !=null \">\n" +
            "and t.create_time    &lt;  str_to_date(DATE_ADD(#{account.endTime},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>\n" +
            "<if test=\"account.name !='' and account.name!=null\">\n" +
            " and t.name LIKE concat('%', #{account.name}, '%') or t.phone LIKE concat('%', #{account.name}, '%')\n" +
            "</if>\n</script>")
    List<AccountDto> findAllByPageable(@Param("account") AccountQueryCriteria criteria);

    /**
     * @Author yzq
     * @Description 根据id查询用户就诊次数，消费总金额
     * @Date 2021/9/3 11:11
     * @Param [id]
     * @return com.zgjkhis.modules.outpatient.dto.AccountDto
     **/
    @Select("<script>select IFNULL(count(1),0) as jzCount,IFNULL(sum(t.total_fee),0) as totalFee from his_prescription_order t\n" +
            "where t.id=#{id}</script>")
    AccountDto findContentById(@Param("id") Long id);
}
